package com.dao.impl;

import com.dao.FinancingDao;
import com.entry.Financing;
import com.entry.Instituty;
import com.pojo.*;
import com.entry.User;
import com.pojo.FinIns;
import com.pojo.SelectFinAndIns;
import com.util.DBHelper;
import com.util.PageHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

public class FinancingDaoImpl implements FinancingDao {
    private DBHelper dbHelper = new DBHelper();



    SimpleDateFormat slf = new SimpleDateFormat("yyyyMMdd");


    @Override
    public ArrayList selectQuery(FinIns finIns, PageHelper pageHelper, double Max, double Min) {
        StringBuilder sql = new StringBuilder("select f.*,i.*  from financing f inner join instituty i on i.instituty_id = f.instituty_id where f.enterprise_id = ? and 1=1 ");
        ArrayList list = new ArrayList();
        list.add(finIns.getEnterprise_id());
        //System.out.println("传过来的机构名是"+finIns.getName());
        if (finIns.getNo() != null) {
            sql.append(" and f.no like ? ");
            list.add("%" + finIns.getNo() + "%");

        }
        if (finIns.getName() != null) {
            sql.append(" and i.name = ? ");
            list.add(finIns.getName());
        }
        if (Max != 0) {
            sql.append(" and f.amount<=? ORDER BY f.amount  ");
            list.add(Max);
        }
        if (Min != 0) {
            sql.append(" and f.amount>=? ");
            list.add(Min);
        }
        if (Min != 0 && Max != 0) {
            sql.append(" and f.amount between ? and ? ");
            list.add(Min);
        }
        if (finIns.getStatus() != null) {
            sql.append(" and f.status=? ");

            list.add(finIns.getStatus());
        }
        if (finIns.getCreate_time() != null) {
            sql.append(" and create_time = ?  ");
            list.add(finIns.getCreate_time());
        }
        sql.append(" limit ? , ? ");
        int startIndex = (pageHelper.getIndexPage() - 1) * pageHelper.getShowData();
        list.add(startIndex);//0
        list.add(pageHelper.getShowData());//4
        //System.out.println("finins的sql语句"+sql.toString());
        return dbHelper.query(sql.toString(), FinIns.class, list.toArray());

    }

    @Override
    public int selectQuerySize(FinIns finIns, PageHelper pageHelper, double Max, double Min) {
        StringBuilder sql = new StringBuilder("select count(*)  from financing f inner join instituty i on i.instituty_id = f.instituty_id where f.enterprise_id = ? and 1=1 ");
        ArrayList list = new ArrayList();
        list.add(finIns.getEnterprise_id());
        if (finIns.getNo() != null) {
            sql.append(" and f.no like ? ");
            list.add("%" + finIns.getNo() + "%");

        }
        if (finIns.getName() != null) {
            sql.append(" and i.name = ? ");
            list.add(finIns.getName());
        }
        if (Max != 0) {
            sql.append(" and f.amount<=? ");
            list.add(Max);
        }
        if (Min != 0) {
            sql.append(" and f.amount>=? ");
            list.add(Min);
        }
        if (Min != 0 && Max != 0) {
            sql.append(" and f.amount between ? and ? ");
            list.add(Min);
        }
        if (finIns.getStatus() != null) {
            sql.append(" and f.status=? ");
            list.add(finIns.getStatus());
        }
        if (finIns.getCreate_time() != null) {
            sql.append(" and STR_TO_DATE(?,'%Y-%m-%d')  ");
            list.add(finIns.getCreate_time());
        }
        return dbHelper.query(sql.toString(),list.toArray());
    }

    @Override
    public FinIns selectQuery(FinIns finIns) {
        String sql = "SELECT f.no,i.name,f.create_time,f.enterprise_id,f.amount,i.interest,f.status,f.fin,f.cost,f.payment_interest_type,f.receipt_time,f.uplink_address,i.receipt_day,f.receipt_amount from financing f INNER JOIN instituty i on f.instituty_id = i.instituty_id where no = ? ";
        ArrayList<FinIns> query = dbHelper.query(sql, FinIns.class, finIns.getNo());

        return query.get(0);
    }


    @Override
    public int insertFinancing(Financing financing) {
        String sql = "update financing set receipt_time=now()+INTERVAL 5 DAY,status='B',amount=?,cost=?, instituty_id=?,receipt_amount=amount-cost where no = ?";
//        financing.setUplink_address(UUID());
//        financing.setNo(getno());
//        System.out.println("修改的Instituty_id是"+financing.getInstituty_id());
        int i = dbHelper.updateTable(sql,
                financing.getAmount(),
                financing.getCost(),
                financing.getInstituty_id(),
                financing.getNo());
//        System.out.println("修改的数据是"+i);
        return i;
    }

    @Override
    public ArrayList selectAll() {
        String sql = "SELECT\n" +
                "financing.receipt_amount,\n" +
                "financing.cost,\n" +
                "instituty.interest,\n" +
                "instituty.receipt_day,\n" +
                "instituty.`name`\n" +
                "FROM\n" +
                "instituty ,\n" +
                "financing";
        ArrayList<SelectFinAndIns> list = dbHelper.query(sql, SelectFinAndIns.class);
        return list;
    }




    public String getno() {
        DBHelper db=new DBHelper();
        Date date = new Date();
        String day = slf.format(date);
        String sql="select MAX(no) as maxno from financing";
        ArrayList<No> max=db.query(sql,No.class);
        String no=max.get(0).getMaxno();
        String last=no.substring(no.length()-6);
        // 初始序列号
        int number = Integer.parseInt(last); // 将序列号转换为整数
        number++; // 增加序列号

        // 使用StringBuilder来构建序列号
        StringBuilder sb = new StringBuilder();
        String formattedNumber = Integer.toString(number);
        for (int i = 0; i < 6 - formattedNumber.length(); i++) {
            sb.append('0');
        }
        sb.append(formattedNumber);
        no = sb.toString();
        return ("R" + day + no);
    }
    public ArrayList<Financing> selectByNo(String no){
        String sql="select * from financing where no =?";
        ArrayList<Financing> list=dbHelper.selectinfo(sql, Financing.class,no);
        return list;
    }


    public String UUID() {
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();
        String uuidString = uid.toString().replace("-", "");
        String id = uuidString.substring(16);
//        System.out.println(id);
        return id;
    }



}
